/******************************************************************************
 * myMutex.cpp - 
 * 
 * Copyright 2011-2016 Guangzhou Timetop Electronics Technology Co.,Ltd.
 * 
 * DESCRIPTION: - 
 *     -
 * modification history
 * 
 * 01a, 21apr2012, Mr. Huanbiao Xie  written
 *
 ******************************************************************************/
#include "Game.h"

CMyMutex::CMyMutex()
{
	m_iMutex=1;
	for(int i=0;i<5;i++)
	{
		m_dwTime[i]=i+2;		
	}
	m_iTimePc=0;
}
CMyMutex::~CMyMutex()
{
}
bool CMyMutex::Lock()
{
	return WaitMutex(1000);
}
bool CMyMutex::UnLock()
{
	m_iMutex=1;
	return true;
}
bool CMyMutex::WaitMutex(int timeout)
{
	DWORD time=GetTickCount();
	while(1)
	{
		//LogDebug("m_iMutex==%d========================",m_iMutex);
		if(GetTickCount()-time>timeout)
		{
			return FALSE;
		}
		//LogDebug("GetTimePc=%d",GetTimePc());
		Sleep(m_dwTime[GetTimePc()]);
		
		if(m_iMutex)
		{
			m_iMutex=0;
			return TRUE;
		}
	}
}
bool CMyMutex::ReleaseMutex()
{
	m_iMutex=1;
	return true;
}
int CMyMutex::GetTimePc()
{
	m_iTimePc++;
	if(m_iTimePc>=5)
	{
		m_iTimePc=0;
	}
	return m_iTimePc;
}
int CMyMutex::GetState()
{
	return m_iMutex;
}
CMyMutex g_CMsgMutex[3];

	
